<?php

class ReservationModel extends Model{
	
	protected $trueTableName  = 'reservation';
	
	//新增
	public function join($data) {
		$map = array();
		$map['customer_id'] = $data['customer_id'];
		$map['project_id'] = $data['project_id'];
		$map['status'] = 1;
		$result = $this->where($map)->find();
		
		if(!empty($result)) {//已预定的就直接返回当前记录的id
			return $result['id'];
		}else{//未预定的进行预定
			$id = $this->add($data);
			return $id;
		}
	}
	
	//综合2个表进行查询详情
	public function getlist() {
		$result = $this->select();
		
		if(!empty($result)) {
			$db = D('Project');
			foreach($result as $k=>$v) {
				$map = array();
				$map['id'] = $v['project_id'];
				$project = $db->where($map)->get();
				if(!empty($project)) {
					unset($project['id']);
					unset($project['status']);
					$temp = array();
					$temp = array_merge($v,$project);
					$result[$k] = $temp;
				}
			}
		}
		
		return $result;
	}
	
	//综合2个表进行查询详情
	public function get() {
		$result = $this->find();
	
		if(!empty($result)) {
			$db = D('Project');
			$map = array();
			$map['id'] = $result['project_id'];
			$project = $db->where($map)->get();
			
			if(!empty($project)) {
				unset($project['id']);
				unset($project['status']);
				$temp = array();
				$temp = array_merge($result,$project);
				$result = $temp;
			}
		}
	
		return $result;
	}
	
	
	//获取未付款或者未完成
	public function getprocessing() {
		$result = $this->order('status desc')->select();
		
		$zhuangtai = '';
		if(!empty($result)) {
			foreach($result as $k=>$v) {
				if($v['status']==2) {
					return array('reservation_id'=>$v['id'],'zhuangtai'=>'done');//正在进行中，不再进行购买
				}
			}
		}
		
		return array('reservation_id'=>'','zhuangtai'=>'buy');//购买
	}
	
}

?>